package com.serkol.ams.controller.api;

import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.bean.Result;
import com.serkol.ams.entity.Navigation;
import com.serkol.ams.service.NavigationService;
import com.serkol.annotation.IsLogin;
import com.serkol.constant.AuthConstant;
import com.serkol.constant.ResultCode;
import com.serkol.exception.AuthException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/navigation")
public class NavigationApiController {
    @Autowired
    private NavigationService navigationService;
    @Value("${upload.host}")
    private String uploadhost;

    @GetMapping("/findByBackList")
    @ResponseBody
    public Result findByBackList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "current", defaultValue = "0") int current) {
        AuthConstant.setPathUrl(uploadhost);
     return navigationService.findByBackList(pageSize, current);

    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Navigation data) {
        boolean flag = false;
        if (data.getId() != null && data.getId() > 0) {
            flag = navigationService.updateById(data);
        } else {
            flag = navigationService.save(data);
        }
        if (flag) {
            return Result.ok();
        } else {
            return Result.failure();
        }

    }
    @GetMapping("/findByMiniList")
    @ResponseBody

    public Result findByMiniList() {
        AuthConstant.setPathUrl(uploadhost);
        List<Map<String, Object>> info = navigationService.findByMiniList();
        return Result.ok(info);
    }

    @IsLogin
    @PostMapping("/byDelete")
    @ResponseBody
    public Result byDelete(@RequestParam(value = "id", defaultValue = "0") long id) throws AuthException {
        if(id>0) {
            boolean b = navigationService.byDelete(id);
            if (b) {
                return Result.ok();
            } else {
                throw new AuthException(ResultCode.REQUEST_ERROR_NOT_ACCEPTABLE);
            }
        }else{
            throw new AuthException(ResultCode.REQUEST_ERROR_NOT_FOUND);
        }
    }
}
